package model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;

import physics.Geometry;
import physics.LineSegment;
import physics.Vect;

public class Absorber implements IGizmo{
        
        private int L;
        private int x;
        private int y;
        private String name;
        private Color col;
        private LineSegment[] lines;
        private physics.Circle[] corners;
        private Vect reflection = null;
        
        public Absorber(int gridL, String gizmoName) {
                L = gridL;
                col = new Color(0xFF00FF);
        }
        
        @Override
        public void addLinkedKey(int key) {
                // TODO Auto-generated method stub
                
        }


        @Override
        public void addLinkedGizmo(IGizmo giz) {
                // TODO Auto-generated method stub
                
        }
        
        @Override
        public void doLinkedAction() {
                // TODO Auto-generated method stub
                
        }

        @Override
        public Rectangle getBounds() {
                return new Rectangle(x, y, L, L);
        }

        @Override
        public Color getColor() {
                return col;
        }

        @Override
        public ArrayList<IGizmo> getLinkedGizmos() {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public ArrayList<Integer> getLinkedKeys() {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public String getType() {
                // TODO Auto-generated method stub
                return "Absorber";
        }

        @Override
        public int getX() {
                return x;
        }

        @Override
        public int getY() {
                return y;
        }

        @Override
        public Graphics paintShape(Graphics g) {
                g.setColor(col);
                g.fillRect(x, y, L, L); 
                return g;
        }

        @Override
        public Vect reflectionVector(Ball ball) {
                return reflection;
        }

        @Override
        public void rotateClockwise() {
                // TODO Auto-generated method stub
                
        }

        @Override
        public void setColor(Color col) {
                this.col = col;
        }

        @Override
        public void setPosition(int xpos, int ypos) {
                x = xpos * L; //top left corner in pixels
                y = ypos * L; //top left corner in pixels
                lines = new LineSegment[4];
//                lines[0] = new LineSegment(0, 475, 25, 475);        //top
//                lines[1] = new LineSegment(0, 475, 0, 500);        //left
//                lines[2] = new LineSegment(500, 475, 500, 500);        //right
//                lines[3] = new LineSegment(475, 500, 500, 500);        //bottom
                
                lines[0] = new LineSegment(x, y, x+L, y);        //top
                lines[1] = new LineSegment(x, y, x, y+L);        //left
                lines[2] = new LineSegment(x+L, y, x+L, y+L);        //right
                lines[3] = new LineSegment(x, y+L, x+L, y+L);        //bottom
                
                corners = new physics.Circle[4];
                corners[0] = new physics.Circle(x, y, 0);
                corners[1] = new physics.Circle(x+L, y, 0);
                corners[2] = new physics.Circle(x+L, y+L, 0);
                corners[3] = new physics.Circle(x, y+L, 0);
                
        }

        @Override
        public double timeUntilCollision(Ball ball) {
                double time=Double.POSITIVE_INFINITY, temp=0;

                for (LineSegment line : lines) {
                        temp = Geometry.timeUntilWallCollision(line, ball.getCircle(), ball.getV());
                        if (temp < time) {
                                time = temp;
                                reflection = Geometry.reflectWall(line, ball.getV()); 
                        }
                }

                for (physics.Circle corner : corners) {
                        temp = Geometry.timeUntilCircleCollision(corner, ball.getCircle(), ball.getV());
                        if (temp < time) {
                                time = temp;
                                reflection = Geometry.reflectCircle(corner.getCenter(),
ball.getCircle().getCenter(), ball.getV());
                        }
                }
                return time;
        }

        @Override
        public void triggerEvent() {
                // TODO Auto-generated method stub
                
        }

        @Override
        public String getName() {
                return name;
        }

		@Override
		public int getTriangleRotations() {
			// TODO Auto-generated method stub
			return 0;
		}
        

}
